package com.cityfood.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.pojo.CfCity;
import com.cityfood.pojo.CfStation;
import com.cityfood.service.CityService;
import com.cityfood.service.StationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class StationController {

	@Autowired
	private StationService stationService;
	
	@Autowired
	private CityService cityService;
	
	/**
	 * 获取城市列表根据城市名称模糊查询
	 */
	@RequestMapping(value="/manage/station",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getStationLikeName(@RequestParam(defaultValue="")String cityName,
											 @RequestParam(defaultValue="")String stationName,
											 @RequestParam(defaultValue="1")Integer currentPage){
		CityFoodResult cityfoodResult = new CityFoodResult();
		PageInfo<CfStation> stationList = null;
		if (cityName.equals("")) {
			//设置分页的页数和行数
			PageHelper.startPage(currentPage, 20);
			List<CfStation> list = stationService.getAllStationLikeName(stationName);
			stationList = new PageInfo<>(list);
		}else{
			
			List<CfCity> cityList = cityService.getCityLikeCityName(cityName);
			List<Integer> cityIds = new ArrayList<>(); 
			for (CfCity city : cityList) {
				cityIds.add(city.getCityId());
			}
			stationList = stationService.getStationByCityIdAndPage(cityIds,stationName,currentPage);
		}
		
		if (stationList!=null) {
			if(stationList.getList().size()==0){
				cityfoodResult.setCode(201);
				cityfoodResult.setMsg("该城市暂无高铁站");
				cityfoodResult.setData("{}");
				return cityfoodResult;
			}
			List<Map<String,Object>> ls = new ArrayList<>();
			for (CfStation station : stationList.getList()) {
				Map<String,Object> mp =new LinkedHashMap<>();
				mp.put("id", station.getId());
				mp.put("name", station.getName());
				mp.put("cityId", station.getCityId());
				//根据高铁站城市id查询到该城市
				CfCity city = cityService.getCityByCityId(station.getCityId());
				
				mp.put("cityName", city.getCityName());
				mp.put("deliverPoint", station.getDeliverPoint());
				mp.put("rallyPoint", station.getRallyPoint());
				mp.put("sort", station.getSort());
				mp.put("display", station.getDisplay());
				ls.add(mp);
			}
			
			
			Map<String,Object> mp = new LinkedHashMap<>();
			//存入查询到的总数
			mp.put("total", stationList.getTotal());
			//存入需要展示的数据
			mp.put("stations", ls);
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取高铁站列表成功");
			cityfoodResult.setData(mp);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
	}
	
	/**
	 * 添加高铁站
	 */
	@RequestMapping(value="/manage/station",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertStation(@RequestBody Map<String,Object> map){
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		Integer cityId = Integer.parseInt(map.get("cityId").toString());
		String name = (String)map.get("name");
		String deliverPoint = (String)map.get("deliverPoint");
		String rallyPoint = (String)map.get("rallyPoint");
		Integer sort = 0 ;
		if (map.containsKey("sort")) {
			sort = Integer.parseInt(map.get("sort").toString());
		}
		Byte display = 1;
		if (map.containsKey("isDisplay")) {
			display = Byte.parseByte(map.get("isDisplay").toString());
		}
		
		CfStation station = new CfStation();
		station.setCityId(cityId);
		station.setName(name);
		station.setDeliverPoint(deliverPoint);
		station.setRallyPoint(rallyPoint);
		station.setSort(sort);
		station.setDisplay(display);
		
		int i = stationService.insertStation(station);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("添加高铁站成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("添加高铁站失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	/**
	 * 添加高铁站
	 */
	@RequestMapping(value="/manage/station",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateStation(@RequestBody Map<String,Object> map){
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		Integer id = Integer.parseInt(map.get("id").toString());
		Integer cityId = Integer.parseInt(map.get("cityId").toString());
		String name = (String)map.get("name");
		String deliverPoint = (String)map.get("deliverPoint");
		String rallyPoint = (String)map.get("rallyPoint");
		Integer sort = 0 ;
		if (map.containsKey("sort")) {
			sort = Integer.parseInt(map.get("sort").toString());
		}
		Byte display = 1;
		if (map.containsKey("isDisplay")) {
			display = Byte.parseByte(map.get("isDisplay").toString());
		}
		
		CfStation station = new CfStation();
		station.setId(id);
		station.setCityId(cityId);
		station.setName(name);
		station.setDeliverPoint(deliverPoint);
		station.setRallyPoint(rallyPoint);
		station.setSort(sort);
		station.setDisplay(display);
		
		int i = stationService.updateStation(station);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("修改高铁站成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("修改高铁站失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	
	/**
	 * 添加高铁站
	 */
	@RequestMapping(value="/manage/station/remove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deletetStation(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfStation station=new CfStation();
		station.setId(Integer.parseInt(id));
		int i = stationService.deletetStation(station);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("删除高铁站成功");
			cityfoodResult.setData(true);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("删除高铁站失败");
			cityfoodResult.setData(false);
			return cityfoodResult;
		}
	}
	
	/**
	 * 添加高铁站
	 */
	@RequestMapping(value="/manage/station/batchremove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deletetStations(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			CfStation station=new CfStation();
			station.setId(Integer.parseInt(strId));
			int i = stationService.deletetStation(station);
			if (i>0) {
				cityfoodResult.setCode(200);
				cityfoodResult.setMsg("删除高铁站成功");
				cityfoodResult.setData(true);
			}else{
				cityfoodResult.setCode(201);
				cityfoodResult.setMsg("删除高铁站失败");
				cityfoodResult.setData(false);
				return cityfoodResult;
			}
		}
		return cityfoodResult;
	}
	
}
